package com.amigo.online.provider.merchant.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.LongTypeHandler;
import org.springframework.stereotype.Component;

import com.amigo.online.common.core.mybatis.MyMapper;
import com.amigo.online.common.param.common.KeyValueModel;
import com.amigo.online.common.param.result.merchant.MerchantCacheInfo;
import com.amigo.online.common.param.result.merchant.MerchantHomeInfo;
import com.amigo.online.provider.merchant.model.entity.Merchant;

@Mapper
@Component
public interface MerchantMapper extends MyMapper<Merchant> {

    List<Integer> getCounts(@Param("userId") Long userId);

    List<Merchant> getConcernList(@Param("userId") Long userId, @Param("start") Integer start,
                                  @Param("size") Integer size);

    Integer getUserMerchantStatus(Long id);

    @Update("update merchant_tbl set grade = #{grade} where id = #{id}")
    int updateGradeById(@Param("id") Long id, @Param("grade") Double grade);

    MerchantHomeInfo getMerchantHome(@Param("merchantId") Long merchantId, @Param("name") String language);

    @Select("select count(*) from merchant_concern_tbl where user_id = #{userId} and merchant_id = #{merchantId} and deleted = 0")
    int getCountByMerchantIdAndUserId(@Param("merchantId") Long merchantId, @Param("userId") Long userId);

    @Select("select DISTINCT c.id,c.${language} as name from goods_tbl g join category_tbl c on g.category_id = c.id and g.`status`=2 where g.merchant_id = #{merchantId}")
    @Results({@Result(column = "id", property = "id", javaType = Long.class, typeHandler = LongTypeHandler.class),
            @Result(column = "name", property = "value", javaType = String.class)})
    List<KeyValueModel<Long, String>> getCategoryByMerchantId(@Param("merchantId") Long merchantId,
                                                              @Param("language") String language);

    @Select("select count(*) from merchant_tbl where id = #{merchantId} and user_id = #{userId}")
    Integer checkManager(@Param("merchantId") Long merchantId, @Param("userId") Long userId);

    @Select("select id, merchant_name as merchantName, lng, lat, merchant_avatar as merchantAvatar, category_id as categoryId,"
            + " status from merchant_tbl where id = #{merchantId} and status = 2")
    MerchantCacheInfo findMerchantCacheInfoById(@Param("merchantId") Long merchantId);

    MerchantHomeInfo getMerchantHomeByUserId(@Param("userId") Long userId,
                                             @Param("name") String language,
                                             @Param("status") Integer status);

    List<MerchantHomeInfo> getMerchantInfoList(@Param("userId") Long userId);

    List<Integer> getCountsByMerchantId(@Param("merchantId") Long merchantId);

    @Select("select count(*) from merchant_tbl where user_id = #{userId} and (status = 2 or status = 1) and deleted = 0")
    Integer checkMerchantKeeper(@Param("userId") Long userId);

    @Select("select user_id from goods_tbl g join  merchant_tbl m on g.merchant_id = m.id where g.id = #{goodsId}")
    Long getUserIdByGoodsId(Long goodsId);

    @Select("select id from merchant_tbl where user_id = #{userId} and id = #{merchantId} and status = 2 and deleted = 0")
    Long checkUserIfOwner(@Param("merchantId") long merchantId, @Param("userId") long userId);

    Merchant selectApplyMerchant(Merchant merchant);

    List<MerchantHomeInfo> findHotMerchant(@Param("name") String name, @Param("label") Integer label, @Param("start") Integer start, @Param("size") Integer size);

    List<MerchantHomeInfo> findRecommendMerchant(@Param("name") String name, @Param("label") Integer label, @Param("start") Integer start, @Param("size") Integer size);


}